package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QXMyCenterService;
import com.tanhua.server.service.UserService;
import com.tanhua.server.vo.CountsVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("users")
public class MyCenterController {

    @Autowired
    private QXMyCenterService QXMyCenterService;

    @Autowired
    private UserService userService;

    /**
     * 在我的这个模块中通过用户ID查询个人信息
     *
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> findUserById(@RequestParam(value = "userID", required = false) Long userId) {
        try {
            UserInfoVo userInfoVo = QXMyCenterService.findUserById(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据用户id查询用户信息出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新用户的个人信息
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Object> updateInfo(@RequestBody UserInfoVo userInfoVo) {
        Boolean result = QXMyCenterService.updateInfo(userInfoVo);
        if (result) {
            return ResponseEntity.ok(null);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity<CountsVo> queryCounts() {
        try {
            CountsVo countsVo = QXMyCenterService.findCounts();
            if (ObjectUtil.isNotEmpty(countsVo)) {
                return ResponseEntity.ok(countsVo);
            }
        } catch (Exception e) {
            log.error("互相喜欢，喜欢，粉丝 - 统计信息失败! userID = " + UserThreadLocal.get().getId(), e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *
     * @param type     1 互相关注 2 我关注 3 粉丝 4 谁看过我
     * @param nickname
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity<Object> relationCountList(@PathVariable(value = "type") String type,
                                                        @RequestParam(value = "nickname", required = false) String nickname,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pageSize) {

        try {
            page = Math.max(1, page);//返回两个int值中的较大者
            PageResult pageResult = QXMyCenterService.findRelationCountList(Convert.toInt(type), nickname, page, pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 取消喜欢
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity<Object> cancelLike(@PathVariable(value = "uid") Long userId) {
        try {
            QXMyCenterService.cancelLike(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 关注粉丝
     *
     * @param userId
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Object> likeFan(@PathVariable("uid") Long userId){
        try {
            this.QXMyCenterService.likeFan(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 查询配置
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<Object> querySettings() {
        try {
            SettingsVo settingsVo = this.QXMyCenterService.findSettings();
            if (null != settingsVo) {
                return ResponseEntity.ok(settingsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 设置陌生人问题
     *
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity<Object> saveQuestions(@RequestBody Map<String, String> param) {
        try {
            String content = param.get("content");
            this.QXMyCenterService.saveQuestions(content);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 查询黑名单
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlacklist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        try {
            PageResult pageResult = this.QXMyCenterService.findBlacklist(page, pagesize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 移除黑名单
     *
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Object> delBlacklist(@PathVariable("uid") Long userId) {
        try {
            this.QXMyCenterService.delBlacklist(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 更新通知设置
     *
     * @param param
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> updateNotification(@RequestBody Map<String, Boolean> param) {
        try {
            Boolean likeNotification = param.get("likeNotification");
            Boolean pinglunNotification = param.get("pinglunNotification");
            Boolean gonggaoNotification = param.get("gonggaoNotification");

            this.QXMyCenterService.updateNotification(likeNotification, pinglunNotification, gonggaoNotification);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
